package com.GlobalPaint.app.ui.Home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.GridViewAdapter;
import com.GlobalPaint.app.Adapter.HomeAdapter;
import com.GlobalPaint.app.Adapter.MyAdapter;
import com.GlobalPaint.app.Adapter.NewHomeAdapter;
import com.GlobalPaint.app.Base.BaseFragment;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.General;
import com.GlobalPaint.app.bean.LoopPicturebean;
import com.GlobalPaint.app.bean.PaletteDetailsBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.HTML.CommonHTML;
import com.GlobalPaint.app.ui.HTML.kongbaiActivity;
import com.GlobalPaint.app.ui.Home.friend.Activity_Message;
import com.GlobalPaint.app.ui.Home.talent.TalentActivity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.GlobalPaint.app.utils.LoopPicture;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends BaseFragment {
    private final String TAG = HomeFragment.class.getSimpleName();
    /**Gridview*/
    private GridView mGridView;
    /**listview*/
    private XRecyclerView mListView;
    // private List<Schooldtbean> mArrayList;
    //包裹list上边的全部布局的view
    private LinearLayout ll_header;
    private int[] icon = {R.drawable.home_hy,R.drawable.home_sb,R.drawable.home_js,R.drawable.home_tl,R.drawable.home_dt,
            R.drawable.home_qy,R.drawable.home_rc,R.drawable.home_qb
    };

    private String[] text = {"圈子","色板样品","最新技术交流","涂料产品",
            "行业动态资讯","企业展示","人才库","全部"};
    private List<General> mGeneralList;//要显示的数据集合
    private ListView lvGenerals;//ListView对象
    /**gridview的条目点击事件*/
    private MyGridViewItemClickListener mItemClickListener;
    private ImageView iv_goback;
    private TextView tv_common_center;
    private LoopPicture loopPicture;
    private ArrayList<String> mArrayList;
    private NewHomeAdapter newHomeAdapter;
    private int page;
    private LoopPicturebean loopPicturebean;
    private List<LoopPicturebean.DataListBean> loopPicturebean_list;
    private String[] img;
    private ImageView iv_saomiao;
    private List<PaletteDetailsBean.DataBean> list;
    private HomeAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        return view;
    }

    /**
     * 初始化view
     * @param view
     */
    private void initView(View view) {
        iv_goback = (ImageView)view. findViewById(R.id.iv_goback);
        tv_common_center = (TextView)view. findViewById(R.id.tv_common_center);
        tv_common_center.setText("全部");
        iv_goback.setVisibility(View.GONE);
        mListView = (XRecyclerView) view.findViewById(R.id.listview);
        mListView.setLoadingMoreEnabled(false);
        mListView.setPullRefreshEnabled(false);
        mListView.setLayoutManager(new LinearLayoutManager(getContext()));
        MyAdapter myAdapter = new MyAdapter();
        mListView.setAdapter(myAdapter);
        //添加头布局
        LayoutInflater lif = (LayoutInflater) App.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headerView =lif.inflate(R.layout.home_listview_header,null,false);

        //初始化要显示的数据集合---start
        mGeneralList = new ArrayList<General>();
        for (int i =0;i<icon.length;i++){
            General general = new General();
            general.setIcon(icon[i]);
            general.setText(text[i]);
            mGeneralList.add(general);
        }

        mGridView = (GridView) headerView.findViewById(R.id.gridview);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        //设置gridview的适配器,通过构造传过去数据
        mGridView.setAdapter(new GridViewAdapter(mGeneralList));
        loopPicture = (LoopPicture)headerView.findViewById(R.id.loopPicture);

        //添加轮播图
        String[] imagesRes = new String[] {
                "drawable://" + R.drawable.ad1,"drawable://" + R.drawable.ad4
        };
        loopPicture.setImageUrl(imagesRes, true);
        if (imagesRes.length == 1) {
            loopPicture.setStopCarousel();
        }
        String[] titles = {"","",
                "","",""};
        loopPicture.setImageTitle(titles);

        //给listview添加header
        mListView.addHeaderView(headerView);
        // 设置listview的适配器
//        mListView.setAdapter(new HomeListViewAdapter(mArrayList));

        mItemClickListener = new MyGridViewItemClickListener();
        //设置gridview的条目点击事件
        mGridView.setOnItemClickListener(mItemClickListener);
        Newest();
    }
    private void Newest(){
        String url= Constants.JASON_SERVICE_URL+"/renren-security/appapi/info/queryNewest";
        DataManager.getInst().getHttpRequestJsonClass(url, "dd", PaletteDetailsBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                PaletteDetailsBean paletteBean=(PaletteDetailsBean)o;
                if (paletteBean.getStatus() == 1){
                    if(paletteBean.getData() != null && paletteBean.getData().size()>0){
                        list=paletteBean.getData();
                        adapter=new HomeAdapter(list,getContext());
                        mListView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                    else {
                        Toast.makeText(getContext(),"获取失败",Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(getContext(),"获取失败",Toast.LENGTH_LONG).show();
                }
                mListView.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if(list!=null && list.size()>0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    mListView.refreshComplete();

                }
            }
        });
    }
    class MyGridViewItemClickListener implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = null;
            switch (position){
              //好友
                case 0:
                    intent = new Intent(getContext(), Activity_Message.class);
                    startActivity(intent);

                    break;
            //色板样品
                case 1:
                    DataManager.typeId = 121;
                    DataManager.typename = "图集";//图集
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                case 3:
                    intent = new Intent(getContext(), kongbaiActivity.class);
                    startActivity(intent);

                    break;

                case 2:
                    intent = new Intent(getContext(), kongbaiActivity.class);
                    startActivity(intent);

                    break;

                case 4:
                    DataManager.typeId = 114;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;

                case 5:
                    DataManager.typeId = 124;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;

                case 6:
                    intent = new Intent(getContext(), TalentActivity.class);
                    startActivity(intent);
                    break;
                //全部
                case 7:
                    intent = new Intent(getContext(), QuanbuActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //页面不可见的时候移除
        loopPicture.setStopCarousel();
    }

}
